package com.hucheng.rest.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.hucheng.common.exception.EduException;
import com.hucheng.common.response.ServiceCoreVO;
import com.hucheng.common.utils.ValidatorUtils;
import com.hucheng.order.service.OrderService;
import com.hucheng.order.vo.BalanceOrderVO;
import com.hucheng.order.vo.OrderInfoVO;
import com.hucheng.rest.base.BaseAction;
import org.springframework.stereotype.Controller;

import java.io.UnsupportedEncodingException;

/**
 * @Author: 凉白开不加冰
 * @Version: 0.0.1V
 * @Date: 2019-07-17
 * @Description: 订单
 **/
@Controller("orderAction")
public class OrderAction extends BaseAction {

    @Reference
    private OrderService orderService;

    /**
     * 下单支付接口
     *
     * @return
     * @throws EduException
     */
    public ServiceCoreVO submitOrderV100() throws EduException, AlipayApiException, UnsupportedEncodingException {
        OrderInfoVO orderInfoVO = orderService.submitOrder(userId);
        JSONObject result = new JSONObject();
        result.put("data", orderInfoVO);
        return response(result);
    }

    /**
     * 获取缓存结算页信息
     *
     * @return
     */
    public ServiceCoreVO getBalanceOrderV100() throws EduException {
        //结算分两种，一种是从购物车结算，一种是直接购买结算
        String cartIds = getRequestContentValue("cart_ids");

        String goodsId = getRequestContentValue("goods_id");

        String goodsNumber = getRequestContentValue("goods_number");

        if(ValidatorUtils.isEmpty(goodsNumber)){
            goodsNumber = "1";
        }

        if (ValidatorUtils.isNull(goodsId)) {
            goodsId = "0";
        }

        BalanceOrderVO balanceOrderVO = orderService.getBalanceOrder(userId, cartIds, Integer.parseInt(goodsId),Integer.parseInt(goodsNumber));
        return response(balanceOrderVO);
    }

    /**
     * 修改结算页信息
     *
     * @return
     */
    public ServiceCoreVO updateBalanceOrderV100() throws EduException {

        String couponId = getRequestContentValue("coupon_id");


        return response();
    }

    /**
     * 查询支付宝订单
     *
     * @return
     */
    public ServiceCoreVO queryAliOrderV100() throws EduException, UnsupportedEncodingException, AlipayApiException {

        String out_trade_no = getRequestContentValue("out_trade_no");
        String orderInfo = orderService.queryAliOrder(out_trade_no);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("order_info", orderInfo);
        return response(jsonObject);
    }

    /**
     * 扫码支付成功后，定时查询支付宝订单
     *
     * @return
     */
    public ServiceCoreVO updateOrderStatusV100() {
        orderService.updateOrderStatus();
        return response();
    }

}
